<?php

class PostsController extends AppController {
	public $helpers = array('Form', 'Html', 'Form');
	
	public function index(){
		$this->set('posts',$this->Post->find('all'));
	}
	
	public function readpost($id = null){
		//$this->Posts->id=$id;
		$this->set('post',$this->Post->read(null,$id));
	}
	
	public function createpost() {
		//debug($this->request->data);
		if ($this->request->is('post')) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }
    
    public function updatepost($id = null){
    	//debug($this->request->data);
		
    	if ($this->request->is('get')){
    		//$this->set('post',$this->Post->read(null,$id));
			
    		$this->request->data = $this->Post->read(null,$id);
		}

		if ($this->request->is('post')) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Your post has been updated.');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Unable to update your post.');
            }
        }		
    }
    
    public function deletepost($id = null){
        if ($this->request->is('get')) {
        	throw new MethodNotAllowedException();
    	}
	    if ($this->Post->delete($id)) {
	        $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
	        $this->redirect(array('action' => 'index'));
	    }
    }
}